//
//  MyTabbar.m
//  UITabbarControllerDemo
//
//  Created by Visitor on 15/12/8.
//  Copyright © 2015年 Visitor. All rights reserved.
//

#import "MyTabbar.h"
#import "LGTabBarButton.h"
#import "UIImage+Image.h"
@implementation MyTabbar
{
    UITabBarController *_tbc;
    UIButton * _selectedBtn;
}

static MyTabbar *_sharedMyTabbar;
+ (MyTabbar *)sharedMyTabbar
{
    if(!_sharedMyTabbar)
    {
        _sharedMyTabbar = [[MyTabbar alloc] init];
    }
    return _sharedMyTabbar;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, [[UIScreen mainScreen] bounds].size.height-49, [[UIScreen mainScreen] bounds].size.width, 49);
    }
    return self;
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

- (void)createMyTabbarWithTabbarController:(UITabBarController *)tbc
{
    _tbc = tbc;
    
    // 1.解析plist文件
    NSDictionary *plistDict = [[NSDictionary alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/TabbarController.plist",[[NSBundle mainBundle] resourcePath]]];
    
    
    NSArray *itemsArray = [plistDict objectForKey:@"items"];
    int index = 0;
    for(NSDictionary *item in itemsArray)
    {
        [self createItemWithItemDict:item andCount:[itemsArray count] andIndex:index];
        index++;
    }
    
}


// 创建按钮
- (void)createItemWithItemDict:(NSDictionary *)itemDict andCount:(NSInteger)count andIndex:(int)index
{
    // 容器
    UIView *baseView = [[UIView alloc] init];
    baseView.frame = CGRectMake(self.frame.size.width/count*index, 0, self.frame.size.width/count, self.frame.size.height);
    baseView.tag = index;
    [self addSubview:baseView];
    
    
    // 按钮
    
    LGTabBarButton *btn = [LGTabBarButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, baseView.frame.size.width, baseView.frame.size.height);
    
    [btn setImage:[UIImage imageNamed:[itemDict objectForKey:@"image"]] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:[itemDict objectForKey:@"selectimage"]] forState:UIControlStateSelected];
    [btn setTitle:itemDict[@"title"] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor colorWithRed:0.84f green:0.22f blue:0.22f alpha:1.00f] forState:UIControlStateSelected];
    
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    btn.tag = index;
    [baseView addSubview:btn];
    
    

     if(index == 0)
     {
         [self btnClick:btn];

     }
}

- (void)btnClick:(UIButton *)btn
{
    _selectedBtn.selected =NO;
    _tbc.selectedIndex = btn.tag;
    btn.selected = YES;
    _selectedBtn = btn;

}


-(void)selectIndex:(NSInteger)index
{
    for (UIView * view in self.subviews) {
        if (view.tag==index) {
            [self btnClick:(UIButton *)[view.subviews lastObject]];
        }
    }
    
}








@end
